<?php
class CategoryModel extends Model{
	  public $table="category";
	  public  $validate=array(
                       array("cname","nonull","栏目名称不能为空",2,3),
                       array("pid","checkPid",":(PID错误！",2,3)
                   	);     
	  //验证pid
	  public function checkPid($name,$value,$msg){
	  	$pid=intval($value);
	  	if($pid===0)return true;
	  	//通过缓存判断pid是否存在、、、
	  	$cateCategory=cache("category");
	  	if(isset($cateCategory[$value])){
	  		return true;
	  	}else{
	  		return $this->error=$msg;
	  	}
                     
	  }
	  //往商品栏目表里面添加数据
            public function addCategory(){
	            	if($this->create()){
	                              $innerkey=$this->add();
	                              if($this->updataCategory()){
	                                   return true;
	                              }else{
                                             return false;
	                              }  
	            	}else{
	            		return false;
	            	}
                    }
              //更新缓存文件
              public function updataCategory(){
              	  $cateCategory=$this->getField("cid,cname,pid,keywords,description,cattype,unit,pricerange,gtid");
              	    $cateCategory=Data::tree($cateCategory,"cname");
              	    if(cache("category",$cateCategory)){
                                  return true;
              	    }else{
              	    	$this->error="添加缓存失败。。。";
              	    	return false;
              	    }
              }
            //编辑栏目
            public function edit(){
            	   if($this->create()){
                   $this->save();
                   if($this->updataCategory()){
                        return true;
                   }
                 }else{
                  return false;
                 }
            }
           //删除栏目
           public function delCategory(){
           	$cid=Q("cid");
           	$cateCategory=cache("category");
           	if(isset($cateCategory[$cid])){
           		while($this->where(array("cid"=>$cid))->del()){
                                  $cid=$this->where(array("pid"=>$cid))->getField("cid",true);
                                  if(!$cid)break;
	           	 }
                     $this->updataCategory();
	              return true;
	           	}else{
	           	     $this->error="参数不对。。。";
	           	}
               	   }

}

